#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2018/1/16 0016 9:32
# @Author  : Arliki
# @email   : hkdnxycz@outlook.com
# @File    : pil_img
from PIL import Image, ImageFont, ImageDraw, ImageFilter
import random


# 大写字母chr(65-90)小写chr(97-122)
def get_letter():
    return chr(random.randint(65, 90))


# 背景颜色
def get_bg_color():
    return (random.randint(32, 127), random.randint(32, 127), random.randint(32, 127))


# 字体颜色
def get_font_color():
    return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255))


def run():
    w = 60 * 4
    h = 60
    image = Image.new('RGB', (w, h), (255, 255, 255))
    # 创建字体对象
    font = ImageFont.truetype(r"C:\Windows\Fonts\SIMLI.TTF", 36)
    # 创建画布
    draw = ImageDraw.Draw(image)
    for x in range(w):
        for y in range(h):
            draw.point((x, y), fill=get_bg_color())
    # 文字
    a = ""
    for f in range(4):
        text = get_letter()
        draw.text((60 * f + 10, 10), text, font=font, fill=get_font_color())
        a += text
    image = image.filter(ImageFilter.EDGE_ENHANCE)
    image.save('test.jpg', 'jpeg')
    print(a)


if __name__ == '__main__':
    run()
